/* * Copyright (C) 2014 Jacob Klinker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.klinker.android.launcher.hello_world; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Point; import android.os.Build; import android.util.TypedValue; import android.view.Display; import android.view.WindowManager; import java.lang.reflect.Method; /** * Common utils functions that you may want or need in your pages */ public class Utils { public static final String PACKAGE_NAME = "com.klinker.android.launcher.hello_world"; private int statusBarHeight = -1; private int navBarHeight = -1; private int actionBarHeight = -1; private Activity context; public Utils(Activity context) { this.context = context; } public int getStatusBarHeight() { if (statusBarHeight == -1) { int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { statusBarHeight = context.getResources().getDimensionPixelSize(resourceId); } } return statusBarHeight; } public int getNavBarHeight() { if (navBarHeight == -1) { int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { navBarHeight = context.getResources().getDimensionPixelSize(resourceId); } } return navBarHeight; } public int getActionBarHeight() { if (actionBarHeight == -1) { TypedValue tv = new TypedValue(); if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); } } return actionBarHeight; } public boolean hasNavBar() { try { Class c = Class.forName("android.os.SystemProperties"); Method m = c.getDeclaredMethod("get", String.class); m.setAccessible(true); String result = (String) m.invoke(null, "qemu.hw.mainkeys"); boolean hasNav = !("1".equals(result)); return hasNav; } catch (Throwable e) { return true; } } public int getListOffset() { return getStatusBarHeight() + getActionBarHeight(); } public boolean isAndroidL() { return Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH; } public boolean isKitKat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } public float toPx(int dp) { Resources r = context.getResources(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); } }